]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
workspace: Refactor layout scale and space calculation
authorJonas Dreßler <verdre@v0yd.nl>
Mon, 1 Feb 2021 10:36:10 +0000 (11:36 +0100)
committerMarge Bot <marge-bot@gnome.org>
Mon, 1 Feb 2021 13:12:43 +0000 (13:12 +0000)
Make computeScaleAndSpace() return an array including scale and space so
we no longer have to access the layout object from outside.

With this we also no longer need to set layout.space, since only the
scale property is needed in computeWindowSlots().

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1617>

js/ui/workspace.js

index c152794d60302fcfc7b5d4a20bf29ef3913a8f72..6a8e999b216af24c3d778221d6abb1125e736e81 100644 (file)
@@ -122,14 +122,15 @@ var LayoutStrategy = class {
         throw new GObject.NotImplementedError(`computeLayout in ${this.constructor.name}`);
     }
 
-    // Given @layout, compute the overall scale and space of the layout.
+    // Given @layout and @area, compute the overall scale of the layout and
+    // space occupied by the layout.
     //
-    // This method does not return anything, but instead installs
-    // the properties "scale" and "space" on @layout directly.
+    // This method returns an array where the first element is the scale and
+    // the second element is the space.
     //
-    // Make sure to call this methods before calling computeWindowSlots(),
-    // as it depends on the scale property installed in @layout here.
-    computeScaleAndSpace(_layout) {
+    // This method must be called before calling computeWindowSlots(), as it
+    // sets the fixed overall scale of the layout.
+    computeScaleAndSpace(_layout, _area) {
         throw new GObject.NotImplementedError(`computeScaleAndSpace in ${this.constructor.name}`);
     }
 
@@ -272,9 +273,7 @@ var UnalignedLayoutStrategy = class extends LayoutStrategy {
         };
     }
 
-    computeScaleAndSpace(layout) {
-        let area = layout.area;
-
+    computeScaleAndSpace(layout, area) {
         let hspacing = (layout.maxColumns - 1) * this._columnSpacing;
         let vspacing = (layout.numRows - 1) * this._rowSpacing;
 
@@ -293,7 +292,8 @@ var UnalignedLayoutStrategy = class extends LayoutStrategy {
         let space = (scaledLayoutWidth * scaledLayoutHeight) / (area.width * area.height);
 
         layout.scale = scale;
-        layout.space = space;
+
+        return [scale, space];
     }
 
     computeWindowSlots(layout, area) {
@@ -436,20 +436,17 @@ var WorkspaceLayout = GObject.registerClass({
         });
     }
 
-    _isBetterLayout(oldLayout, newLayout) {
-        if (oldLayout.scale === undefined)
-            return true;
-
-        let spacePower = (newLayout.space - oldLayout.space) * LAYOUT_SPACE_WEIGHT;
-        let scalePower = (newLayout.scale - oldLayout.scale) * LAYOUT_SCALE_WEIGHT;
+    _isBetterScaleAndSpace(oldScale, oldSpace, scale, space) {
+        let spacePower = (space - oldSpace) * LAYOUT_SPACE_WEIGHT;
+        let scalePower = (scale - oldScale) * LAYOUT_SCALE_WEIGHT;
 
-        if (newLayout.scale > oldLayout.scale && newLayout.space > oldLayout.space) {
+        if (scale > oldScale && space > oldSpace) {
             // Win win -- better scale and better space
             return true;
-        } else if (newLayout.scale > oldLayout.scale && newLayout.space <= oldLayout.space) {
+        } else if (scale > oldScale && space <= oldSpace) {
             // Keep new layout only if scale gain outweighs aspect space loss
             return scalePower > spacePower;
-        } else if (newLayout.scale <= oldLayout.scale && newLayout.space > oldLayout.space) {
+        } else if (scale <= oldScale && space > oldSpace) {
             // Keep new layout only if aspect space gain outweighs scale loss
             return spacePower > scalePower;
         } else {
@@ -498,6 +495,8 @@ var WorkspaceLayout = GObject.registerClass({
 
         let lastLayout = null;
         let lastNumColumns = -1;
+        let lastScale = 0;
+        let lastSpace = 0;
 
         for (let numRows = 1; ; numRows++) {
             const numColumns = Math.ceil(this._sortedWindows.length / numRows);
@@ -511,16 +510,17 @@ var WorkspaceLayout = GObject.registerClass({
             const layout = strategy.computeLayout(this._sortedWindows, {
                 numRows,
             });
-            layout.area = area;
             layout.strategy = strategy;
 
-            strategy.computeScaleAndSpace(layout);
+            const [scale, space] = strategy.computeScaleAndSpace(layout, area);
 
-            if (!this._isBetterLayout(lastLayout, layout))
+            if (!this._isBetterScaleAndSpace(lastScale, lastSpace, scale, space))
                 break;
 
             lastLayout = layout;
             lastNumColumns = numColumns;
+            lastScale = scale;
+            lastSpace = space;
         }
 
         return lastLayout;